// components/search/search-cmp.js
import {
  HTTP
} from '../../utils/http.js'
import {
  KeywordModel
} from 'keyword.js'
import {
  paginationBev
} from '../behaviors/pagination.js'
let http = new HTTP()
let keyModel = new KeywordModel()

Component({
  /**
   * 组件的属性列表
   */
  behaviors: [paginationBev],
  properties: {
    more: {
      type: String,
      observer: '_loadMore'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // start:0,
    // count:20,
    historyKeys: [],
    hotKeys: [],
    finished: false,
    q: '',
    loading: false,
    loadingCenter: false,
    // description: '',
    active: 0,
    userList: [],
    userPageSize: 10,
    userPageNum: 1,
    userTotal: 0,
    workList: [],
    workPageSize: 10,
    workPageNum: 1,
    workTotal: 0,
    boxList: [],
    boxPageSize: 10,
    boxPageNum: 1,
    boxTotal: 0
  },

  attached: function () {
    keyModel.getHistory((data) => {
      this.setData({
        historyKeys: data.data
      })
    })
    keyModel.getHot((data) => {
      this.setData({
        hotKeys: data.data
      })
    })
  },

  onReachBottom() {
    console.log("onReachBottom");
  },


  /**
   * 组件的方法列表
   * 
   */
  methods: {
    changeTap() {
      keyModel.getHot((data) => {
        this.setData({
          hotKeys: data.data
        })
      })
    },
    _loadMore: function () {
      if (!this.data.q) {
        return
      }
      let hasMore = this.hasMore()
      if (!hasMore) {
        return
      }
      this.setData({
        loading: true
      })
      http.request({
        url: 'book/search?summary=1',
        data: {
          q: this.data.q,
          start: this.getCurrentStart()
        },
        success: (data) => {
          this.setMoreData(data.books)
          this.setData({
            loading: false
          })
        }
      })
    },

    onCancel: function (event) {
      this.triggerEvent('cancel', {}, {})
    },

    onDelete: function (event) {
      this.setData({
        finished: false,
        empty: false,
        q: ''
      })
    },

    handleInputValue(event) {
      this.setData({
        q: event.detail.value
      })
    },

    onSearch: function (event) {
      // 首先切换状态，保持客户端流畅
      this.setData({
        finished: true,
        loadingCenter: true,
        active: 0
      })
      this.getWorksList(this.data.q);
    },

    onChange(event) {
      // 切换时清空所有数据
      this.setData({
        active: event.detail.index,
        userList: [],
        userPageSize: 10,
        userPageNum: 1,
        userTotal: 0,
        workList: [],
        workPageSize: 10,
        workPageNum: 1,
        workTotal: 0,
        boxList: [],
        boxPageSize: 10,
        boxPageNum: 1,
        boxTotal: 0
      })
      if (event.detail.index === 0) {
        // 搜索表情列表
        this.getWorksList(this.data.q);
      } else if (event.detail.index === 1) {
        // 搜索创作盒列表
        this.getBoxsList(this.data.q);
      } else if (event.detail.index === 2) {
        // 搜索用户列表
        this.getUserList(this.data.q);
      }
    },

    getBoxsList(name) {
      http.request({
        url: '/ai/draw/box/get/page',
        method: 'post',
        data: {
          name,
          "pageSize": this.data.boxPageSize,
          "pageNum": this.data.boxPageNum
        },
        success: (res) => {
          if (res.code === "0") {
            let list = res.data.list
            var arr1 = this.data.boxList;
            var arr2 = list;
            arr1 = arr1.concat(arr2);
            this.setData({
              boxList: arr1,
              boxTotal: res.data.total
            })
          }
        }
      })
    },

    getWorksList(description) {
      http.request({
        url: '/ai/draw/picture/get/page',
        method: 'post',
        data: {
          description,
          "pageSize": this.data.workPageSize,
          "pageNum": this.data.workPageNum
        },
        success: (res) => {
          if (res.code === "0") {
            let list = res.data.list
            var arr1 = this.data.workList;
            var arr2 = list;
            arr1 = arr1.concat(arr2);
            this.setData({
              workList: arr1,
              workTotal: res.data.total
            })
          }
        }
      })
    },

    getUserList(accountName) {
      http.request({
        url: '/ai/draw/account/page/list',
        method: 'post',
        data: {
          accountName,
          "pageSize": this.data.userPageSize,
          "pageNum": this.data.userPageNum
        },
        success: (res) => {
          if (res.code === "0") {
            let list = res.data.list
            var arr1 = this.data.userList;
            var arr2 = list;
            arr1 = arr1.concat(arr2);
            this.setData({
              userList: arr1,
              userTotal: res.data.total
            })
          }
        }
      })
    },

    refreshUserList() {
      this.setData({
        userPageSize: 10,
        userPageNum: 1,
        userList: [],
        userTotal: 0
      })
      this.getUserList(this.data.q);
    },

    tagSearch(event) {
      this.setData({
        q: event.detail.text
      })
    },
    wantToSearch(e) {
      this.setData({
        q: e.currentTarget.dataset.text
      })
    },
    /**
     * 父组件调用的页面上拉触底事件的处理函数，作用于分页
     */
    reachBottom() {
      if (this.data.active === 0) {
        // 搜索表情列表
        if (!(this.data.workList && this.data.workList.length > 0)) return
        let currentTotal = this.data.workList.length;
        if (currentTotal <= this.data.workTotal) {
          var current = this.data.workPageNum * 1 + 1;
          this.setData({
            workPageNum: current
          })
          this.getWorksList(this.data.q);
        }
      } else if (this.data.active === 1) {
        // 搜索创作盒列表
        if (!(this.data.boxList && this.data.boxList.length > 0)) return
        let currentTotal = this.data.boxList.length;
        if (currentTotal <= this.data.boxTotal) {
          var current = this.data.boxPageNum * 1 + 1;
          this.setData({
            boxPageNum: current
          })
          this.getBoxsList(this.data.q);
        }
      } else if (this.data.active === 2) {
        // 搜索用户列表
        if (!(this.data.userList && this.data.userList.length > 0)) return
        let currentTotal = this.data.userList.length;
        if (currentTotal <= this.data.userTotal) {
          var current = this.data.userPageNum * 1 + 1;
          this.setData({
            userPageNum: current
          })
          this.getUserList(this.data.q);
        }
      }
    }
  }
})